package com.huirui.server.controller.admin;

import com.huirui.server.pojo.dto.CategoryDto;
import com.huirui.server.pojo.entity.Category;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.*;
import com.huirui.server.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@Validated
@Tag(name = "分类管理", description = "小程序分类管理")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping
    @Operation(summary = "获取分类列表")
    public Result<List<CategoryVo>> getCategoryList() {
        List<CategoryVo> categoryVoList = categoryService.getCategoryList();
        return Result.success(categoryVoList);
    }


    @GetMapping("/complete")
    @Operation(summary = "获取所有具有三级分类的分类列表")
    public Result<List<CategoryVo>> getCompleteCategoryList() {
        List<CategoryVo> categoryVoList = categoryService.getCompleteCategoryList();
        return Result.success(categoryVoList);
    }

    @GetMapping("/top/and/secondary")
    @Operation(summary = "获取前两级分类")
    public Result<List<CategoryVo>> getFirstTwoLevelsOfCategories() {
        List<CategoryVo> categoryVoList = categoryService.getFirstTwoLevelsOfCategories();
        return Result.success(categoryVoList);
    }

    @DeleteMapping
    @Operation(summary = "删除某一分类")
    public Result<Void> deleteCategoryById(@RequestParam Long id) {
        categoryService.deleteCategoryById(id);
        return Result.success();
    }

    @PostMapping
    @Operation(summary = "添加分类")
    public Result<Void> addCategory(@RequestBody CategoryDto categoryDto) {
        categoryService.addCategory(categoryDto);
        return Result.success();
    }

    @GetMapping("/parent/name")
    @Operation(summary = "获取父分类名称")
    public Result<String> getParentName(@RequestParam("id") Long id) {
        String parentName = categoryService
                .lambdaQuery()
                .select(Category::getName)
                .eq(Category::getId, id)
                .one()
                .getName();
        return Result.success(parentName);
    }

    @PutMapping
    @Operation(summary = "修改分类")
    public Result<Void> modifyCategory(@RequestBody CategoryDto categoryDto) {
        categoryService.modifyCategory(categoryDto);
        return Result.success();
    }

    @PatchMapping("/status/{status}")
    @Operation(summary = "修改分类状态")
    public Result<Void> changeCategoryStatus(@RequestParam("id") Long id, @PathVariable("status") Long status) {
        categoryService.changeCategoryStatus(id, status);
        return Result.success();
    }


    @GetMapping("/cascader")
    @Operation(summary = "获取分类列表(具有三级分类的)")
    public Result<List<CategoryCascaderVo>> queryCategoryCascader() {
        List<CategoryCascaderVo> res = categoryService.queryCategoryCascader();
        return Result.success(res);
    }

    @GetMapping("/top/without/ranking")
    @Operation(summary = "获取顶级分类")
    public Result<List<TopCategoryVo>> getTopWithoutRankingCategoryList() {
        List<TopCategoryVo> list = categoryService.listTopWithoutRankingCategories();
        return Result.success(list);
    }

    @PatchMapping("/update/status")
    @Operation(summary = "更新分类状态")
    public Result<Void> updateCategoryStatus(@RequestParam Long categoryId, @RequestParam Long status) {
        categoryService.changeCategoryStatus(categoryId, status);
        return Result.success();
    }

    @GetMapping("/secondary/category")
    @Operation(summary = "获取所有的二级分类")
    public Result<List<SecondaryCategoryVo>> getSecondaryCategoryList() {
        List<SecondaryCategoryVo> list = new ArrayList<>();
        categoryService
                .lambdaQuery()
                .eq(Category::getLevel, 2)
                .select(Category::getId, Category::getName)
                .list()
                .forEach(category -> {
                    SecondaryCategoryVo secondaryCategoryVo = SecondaryCategoryVo
                            .builder()
                            .id(category.getId())
                            .name(category.getName())
                            .build();
                    list.add(secondaryCategoryVo);
                });
        return Result.success(list);
    }

    @GetMapping("/tertiary/category")
    @Operation(summary = "获取所有的三级分类")
    public Result<List<TertiaryCategoryVo>> getTertiaryCategoryList(Long categoryId) {
        List<TertiaryCategoryVo> list = categoryService.listTertiaryCategories(categoryId);
        return Result.success(list);
    }

    @GetMapping("/top/category")
    @Operation(summary = "查询一级分类")
    public Result<List<TopCategoryVo>> listTopCategories() {
        List<TopCategoryVo> categoryVoList = categoryService.listTopCategories();
        return Result.success(categoryVoList);
    }

    @PostMapping("/add/category")
    @Operation(summary = "新增分类")
    public Result<Void> addNewCategory(@RequestBody CategoryDto categoryDto) {
        categoryService.addNewCategory(categoryDto);
        return Result.success();
    }

    @PutMapping("/update/category")
    @Operation(summary = "编辑分类")
    public Result<Void> updateCategory(@RequestBody CategoryDto categoryDto) {
        categoryService.updateCategory(categoryDto);
        return Result.success();
    }

    @DeleteMapping("/delete/category")
    @Operation(summary = "删除分类")
    public Result<Void> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategoryById(id);
        return Result.success();
    }
}
